package com.jxf.demo.util;

import cn.hutool.json.JSONUtil;
import com.jxf.demo.dto.StraightLine;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

public class PolygonFixUtils {

    public static void main(String[] args) {

        List<StraightLine> lines = getLines();
        Map<Integer, LinkedList<StraightLine>> map = new HashMap<>();
        int i = 0;
        int size = lines.size();
        while (!CollectionUtils.isEmpty(lines)) {
            LinkedList<StraightLine> fixedLines = new LinkedList<>();
            StraightLine current = lines.remove(0);
            fixedLines.add(current);
            fixPolygon(lines, fixedLines, current);
            if (size == lines.size()) {
                break;
            }
            map.put(i, fixedLines);
            i++;
        }

        System.out.println(JSONUtil.toJsonStr(map));
    }

    public static void fixPolygon(List<StraightLine> lines, LinkedList<StraightLine> fixedLines, StraightLine current) {
        StraightLine.Coordinate currentEnd = current.getEnd();
        Iterator<StraightLine> iterator = lines.iterator();
        int size = fixedLines.size();
        while (iterator.hasNext()) {
            StraightLine next = iterator.next();
            // 找到下一条线的起点是当前线的终点的线
            if (isEqual(next.getStart(), currentEnd)) {
                fixedLines.add(next);
                iterator.remove();
                break;
            }
            // 找到下一条线的终点是当前线的起点线，并将start和end进行替换
            if (isEqual(next.getEnd(), currentEnd)) {
                StraightLine fixedLine = StraightLine.builder()
                        .start(next.getEnd())
                        .end(next.getStart())
                        .radius(next.getRadius())
                        .c(next.getC())
                        .build();
                fixedLines.add(fixedLine);
                iterator.remove();
                break;
            }
        }
        if (CollectionUtils.isEmpty(lines) || size == fixedLines.size()) {
            return;
        }
        fixPolygon(lines, fixedLines, fixedLines.getLast());

    }

    public static boolean isEqual(StraightLine.Coordinate source, StraightLine.Coordinate target) {
        return source.getX().compareTo(target.getX()) == 0
                && source.getY().compareTo(target.getY()) == 0;
    }

    public static List<StraightLine> getLines() {
        // 多边形1
        StraightLine.Coordinate a = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("1000"), new BigDecimal("1000"));
        StraightLine.Coordinate b = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("2000"), new BigDecimal("1000"));
        StraightLine.Coordinate c = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("2000"), new BigDecimal("1800"));
        StraightLine.Coordinate d = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("1600"), new BigDecimal("2000"));
        StraightLine.Coordinate e = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("1000"), new BigDecimal("2000"));

        // 多边形2
        StraightLine.Coordinate a1 = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("3000"), new BigDecimal("1000"));
        StraightLine.Coordinate b1 = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("4000"), new BigDecimal("1000"));
        StraightLine.Coordinate c1 = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("4000"), new BigDecimal("1600"));
        StraightLine.Coordinate d1 = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("3800"), new BigDecimal("2000"));
        StraightLine.Coordinate e1 = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("3000"), new BigDecimal("2000"));


        // 多边形+弧
        StraightLine.Coordinate f = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("3000"), new BigDecimal("3000"));
        StraightLine.Coordinate g = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("4200"), new BigDecimal("3000"));
        StraightLine.Coordinate h = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("4200"), new BigDecimal("4000"));
        StraightLine.Coordinate i = StraightLine.Coordinate.getPointCoordinate(new BigDecimal("3000"), new BigDecimal("4000"));

        List<StraightLine> lines = new ArrayList<>();

        lines.add(StraightLine.polygon(b, a));
        lines.add(StraightLine.polygon(c, b));
        lines.add(StraightLine.polygon(d, c));
        lines.add(StraightLine.polygon(d, e));
        lines.add(StraightLine.polygon(a, e));

        lines.add(StraightLine.polygon(b1, a1));
        lines.add(StraightLine.polygon(c1, b1));
        lines.add(StraightLine.polygon(d1, c1));
        lines.add(StraightLine.polygon(d1, e1));
        lines.add(StraightLine.polygon(a1, e1));

        lines.add(StraightLine.circle(f, g,new BigDecimal("200")));
        lines.add(StraightLine.polygon(g, h));
        lines.add(StraightLine.polygon(f, i));
        lines.add(StraightLine.circle(h,i,new BigDecimal("200")));

        return lines;
    }
}
